﻿using DAL.Models;
using System;
using System.Collections.Generic;
using System.Linq;

namespace DAL.DBManagers
{
    public class NavigationModesManager : BaseManager<NavigationMode>
    {
        public NavigationModesManager()
            : base()
        {
        }

        public override IEnumerable<NavigationMode> GetAll()
        {
            return base.GetAll().Where(w => w.IsActive && w.FinishDate == null);
        }

        public List<NavigationModeHelper> GetNavigationModes()
        {
            return base.GetAll()
                       .Where(w => w.IsActive && w.FinishDate == null).Select(s => new NavigationModeHelper
                       {
                           RecordID = s.RecordID,
                           ID = s.NavigationModeID,
                           Name = s.Name,
                           NameGeo = s.NameGeo,
                           BeginDate = s.BeginDate,
                           FinishDate = s.FinishDate,
                           IsActive = s.IsActive
                       }).ToList();
        }

        public List<NavigationModeHelper> GetNavigationModes(string sortBy)
        {
            var context = (PortEntities)this._context;

            List<NavigationModeHelper> prodList = base.GetAll()
                       .Where(w => w.IsActive && w.FinishDate == null).Select(s => new NavigationModeHelper
                       {
                           RecordID = s.RecordID,
                           ID = s.NavigationModeID,
                           Name = s.Name,
                           NameGeo = s.NameGeo,
                           BeginDate = s.BeginDate,
                           FinishDate = s.FinishDate,
                           IsActive = s.IsActive
                       }).ToList();
            if (sortBy != "")
            {
                DAL.Models.GenericComparer<NavigationModeHelper> cmp = new DAL.Models.GenericComparer<NavigationModeHelper>(sortBy);
                prodList.Sort(cmp);
            }
            return prodList;
        }

        public List<NavigationModeHelper> GetNavigationModes(Guid NavigationModeID)
        {
            return base.GetAll().Where(w => w.IsActive && w.NavigationModeID == NavigationModeID)
                       .Select(s => new NavigationModeHelper
                       {
                           RecordID = s.RecordID,
                           ID = s.NavigationModeID,
                           Name = s.Name,
                           NameGeo = s.NameGeo,
                           BeginDate = s.BeginDate,
                           FinishDate = s.FinishDate,
                           IsActive = s.IsActive
                       }).OrderByDescending(o => o.BeginDate).ToList();
        }

        public List<NavigationModeHelper> GetNavigationModes(Guid NavigationModeID, string sortBy)
        {
            var prodList = base.GetAll().Where(w => w.IsActive && w.NavigationModeID == NavigationModeID)
                       .Select(s => new NavigationModeHelper
                       {
                           RecordID = s.RecordID,
                           ID = s.NavigationModeID,
                           Name = s.Name,
                           NameGeo = s.NameGeo,
                           BeginDate = s.BeginDate,
                           FinishDate = s.FinishDate,
                           IsActive = s.IsActive
                       }).OrderByDescending(o => o.BeginDate).ToList();
            if (sortBy != "")
            {
                DAL.Models.GenericComparer<NavigationModeHelper> cmp = new DAL.Models.GenericComparer<NavigationModeHelper>(sortBy);
                prodList.Sort(cmp);
            }
            return prodList;
        }

        public void RepiarDates(Guid NavigationModeID)
        {
            var navigationmodes = this.Find(f => f.IsActive && f.NavigationModeID == NavigationModeID).OrderBy(o => o.BeginDate).ToList();

            var count = navigationmodes.Count();

            for (int i = 0; i < count; i++)
            {
                if (i < count - 1)
                {
                    if (navigationmodes[i].FinishDate != navigationmodes[i + 1].BeginDate)
                    {
                        navigationmodes[i].FinishDate = navigationmodes[i + 1].BeginDate;
                    }
                }
                else if (i == count - 1)
                {
                    if (navigationmodes[i].FinishDate != null)
                    {
                        navigationmodes[i].FinishDate = null;
                    }
                }
            }

            SaveChanges();
        }
    }
}
